package com.work.export.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class CustomThreadPoolConfig {

    @Bean("customThreadPool")
    public TaskExecutor customThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);  // 设置核心线程数
        executor.setMaxPoolSize(20);   // 设置最大线程数
        executor.setQueueCapacity(500); // 设置队列容量
        executor.setThreadNamePrefix("ThreadPool-");//配置线程池中的线程的名称前缀
        //设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }
}